import { http } from './http';

export const getCurrentCity = () => {
  const curCity = window.localStorage.getItem('zf_city');
  if (!curCity) {
    return new Promise((resolve, reject) => {
      try {
        const { BMap } = window;
        new BMap.LocalCity().get(async res => {
          const {
            data: { body: city },
          } = await http.get('/area/info', { name: res.name });
          window.localStorage.setItem('zf_city', JSON.stringify(city));
          resolve(city);
        });
      } catch (e) {
        reject(e);
      }
    });
  } else {
    return Promise.resolve(JSON.parse(curCity));
  }
};
